//
//  CNStatusRetweetView.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/27.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import SnapKit

class CNStatusRetweetView: UIView {
    
    var retweetViewBottomConstraint: Constraint?
    
    var retweetStatus: CNStatus? {
        didSet {
            guard let screen_name = retweetStatus?.user?.screen_name else {
                return
            }
            guard let tempText = retweetStatus?.attrText else {
                return
            }
            let attrText = NSMutableAttributedString(string: "@\(screen_name): ")
            attrText.appendAttributedString(tempText)
            retweetContentLabel.attributedText = attrText
            
            retweetViewBottomConstraint?.uninstall()
            if let pictureURLs = retweetStatus?.pictureURLs {
                retweetPictureView.hidden = false
                retweetPictureView.pictureURLs = pictureURLs
                self.snp_makeConstraints(closure: { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView).offset(defaultMargin).constraint
                })
            } else {
                retweetPictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(retweetContentLabel).offset(defaultMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    private func prepareUI() {
        
        addSubview(bgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        bgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(defaultMargin)
            make.trailing.equalTo(self).offset(-defaultMargin)
        }
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(defaultMargin)
            make.leading.equalTo(self).offset(defaultMargin)
            make.size.equalTo(CGSize(width: 200, height: 80))
        }
        self.snp_makeConstraints { (make) -> Void in
            retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView).offset(defaultMargin).constraint
        }
    }
    private lazy var bgButton: UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    
    private lazy var retweetContentLabel: FFLabel = {
        
        let label = FFLabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(textFontLarge)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * defaultMargin
        return label
    }()
    
    private lazy var retweetPictureView: CNStatusPicturesView = CNStatusPicturesView()

}
